package vn.kiemviec.app;
/**
 * TrungTT
 * 20-Mar-2013
 */
import vn.kiemviec.app.fragments.AdvicesFragment;
import vn.kiemviec.app.fragments.AlertFragment;
import vn.kiemviec.app.fragments.HomeFragment;
import vn.kiemviec.app.fragments.MenuFragment;
import vn.kiemviec.app.fragments.NavFragment;
import vn.kiemviec.app.fragments.SearchFragment;
import vn.kiemviec.app.utils.LogTools;
import vn.kiemviec.app.utils.ViewHelper;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

public class MainActivity extends FragmentActivity {

	
	
	public final int TabCount = 4;
	private NavFragment[] tabFragments;
	private Button[] tabBarButtons;
	private Button currentButton;
	
	private NavFragment currentTab;
	private int currentTabIndex = 0;
	
	private boolean IsShowMenu = false;
	private MenuFragment fragMenu;
	private FrameLayout fraContainer;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		fraContainer = ViewHelper.findView(this, R.id.fragContainer);
		
		LinearLayout lnTabbar = ViewHelper.findView(this, R.id.lnearTabbar);
		LogTools.info("h:"+lnTabbar.getHeight());
		
		initTab();
		showTab(0);
	}
	
	private void initTab(){
//		
		fragMenu = new MenuFragment();
		fragMenu.addIntoContainer(R.id.menuContainer, getSupportFragmentManager());
		fragMenu.hideFragment(fragMenu);
		
		tabFragments = new NavFragment[TabCount];
		tabFragments[0] = new HomeFragment();
		tabFragments[1] = new SearchFragment();
		tabFragments[2] = new AlertFragment();
		tabFragments[3] = new AdvicesFragment();
		
		currentTab		= tabFragments[0];
		for(int i = 0;i<TabCount;i++){
			tabFragments[i].addIntoContainer(R.id.fragContainer,getSupportFragmentManager());
			tabFragments[i].hide();
		}
		currentTab.show();
		
		tabBarButtons = new Button[TabCount];
		tabBarButtons[0] = ViewHelper.findView(this, R.id.btnHome);
		tabBarButtons[1] = ViewHelper.findView(this, R.id.btnSearch);
		tabBarButtons[2] = ViewHelper.findView(this, R.id.btnAlert);
		tabBarButtons[3] = ViewHelper.findView(this, R.id.btnAdvises);
		//tabBarButtons[4] = ViewHelper.findView(this, R.id.btnMenu);
		
		currentButton 	= tabBarButtons[0];
		currentButton.setSelected(true);
		
		OnClickListener onTabClick = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				switch (v.getId()) {				
				case R.id.btnHome:
					showTab(0);
					break;
				case R.id.btnSearch:
					showTab(1);
					break;
				case R.id.btnAlert:
					showTab(2);
					break;
				case R.id.btnAdvises:
					showTab(3);
					break;
				default:
					break;
				}
			}
		};
		for(int i = 0;i<TabCount;i++){
			tabBarButtons[i].setOnClickListener(onTabClick);
		}
		
		Button btnMenu = ViewHelper.findView(this, R.id.btnMenu);
		btnMenu.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showMenu();
			}
		});
	}
	
	
	
	public void showTab(int tabIndex){
		if(currentTabIndex == tabIndex) return;
		
		currentButton.setSelected(false);
		currentButton = tabBarButtons[tabIndex];
		currentButton.setSelected(true);
		
		currentTab.hide();
		currentTab = tabFragments[tabIndex];
		currentTab.show();
		
		currentTabIndex = tabIndex;
	}
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
	}
	
	private void showMenu(){
		IsShowMenu = !IsShowMenu;
		if(IsShowMenu){
			fragMenu.showFragment(fragMenu);
			TranslateAnimation anim=new TranslateAnimation(0,384,0,0);
			anim.setFillAfter(true);
			fraContainer.startAnimation(anim);
		}else{
			fragMenu.hideFragment(fragMenu);
			TranslateAnimation anim=new TranslateAnimation(384,0,0,0);
			anim.setFillAfter(true);
			fraContainer.startAnimation(anim);
//			fraContainer.setAnimation(anim);
		}
	}
	
	public void showSearchByIndustry(String id){
		showTab(1);
		SearchFragment fr = (SearchFragment)currentTab;
		fr.showSearchResultByIndustry(id);
	}
	
	public void showSearchByWord(String keyword){
		showTab(1);
		SearchFragment fr = (SearchFragment)currentTab;
		fr.showSearchResultByKeyword(keyword);
	}
}
